﻿package
{
	import flash.display.Loader;
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.events.IEventDispatcher;
	import flash.events.IOErrorEvent;
	import flash.events.ProgressEvent;
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	import flash.net.URLRequestMethod;
	import flash.net.URLVariables;

	public class FacebookService extends EventDispatcher
	{
		private var _request:URLRequest;
		private var _loader:URLLoader;

		private var _serviceURL:String ="http://www.hdtv60.com/demo-game/services/index.php";
 		private var _appAPIkey:String="3e99105ed5c922110fb95eeb507e1599";
 		private var _appSecret:String ="fa104a2e1f7d83328599406adbe7cbf1";
 		
 		
 		private var _sessionKey:String
 		
 		public function FacebookService(sessionKey:String)
		{	
			_request = new URLRequest(_serviceURL);
			_loader = new URLLoader();
			_loader.addEventListener(IOErrorEvent.IO_ERROR, errorHandler);
            _loader.addEventListener(Event.COMPLETE, loaderCompleteHandler);

			_request.method = URLRequestMethod.POST;
			_sessionKey = sessionKey;
		} 
		
		public function getFriends():void
		{
			var variable:URLVariables = new URLVariables();
			variable.session_key = _sessionKey;
			//variable.uid;
			variable.method ="get.freinds";
			
			this._request.data = variable;
			
			try {
                _loader.load(_request);
            }
            catch (error:SecurityError)
            {
                trace("A SecurityError has occurred.");
            }
		}

       private function loaderCompleteHandler(event:Event):void 
       {
			if (_loader.data != null)
			{
                var xml:XML = new XML(_loader.data);
                if(xml.method =="get.freinds")
                {
	                var e:FacebookServiceEvent= new FacebookServiceEvent("getFriends");
	                e.result = xml;
	                this.dispatchEvent(e);
                }
			} 
			else
			{
               trace("Could not parse the XML file.");
			}

        }
     
		private function errorHandler(e:IOErrorEvent):void {
           
           trace("Had problem loading the XML File.");
           
        }

	}
}